//
//  DvdDetailsCollectionViewCell.m
//  SugarPile
//
//  Created by LC on 16/2/29.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "DvdDetailsCollectionViewCell.h"
#import "LCPrefixHeader.pch"

@interface DvdDetailsCollectionViewCell ()
@property (nonatomic, retain)UIImageView *album;
@property (nonatomic, retain)UILabel *topic;
@property (nonatomic, retain)UILabel *number;
@property (nonatomic, retain)UILabel *likeCount;
@property (nonatomic, retain)UILabel *albumType;
@end

@implementation DvdDetailsCollectionViewCell
-(void)dealloc
{
    [_model release];
    [_album release];
    [_topic release];
    [_number release];
    [_likeCount release];
  
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.album = [[UIImageView alloc] init];
        //        _album.backgroundColor = [UIColor greenColor];
        _album.contentMode = UIViewContentModeScaleAspectFill;
        _album.clipsToBounds = YES;
        [self.contentView addSubview:_album];
        [_album release];
        
        self.topic = [[UILabel alloc] init];
        //        _topic.backgroundColor = [UIColor redColor];
        _topic.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_topic];
        [_topic release];
        
        self.number = [[UILabel alloc] init];
        //        _number.backgroundColor = [UIColor yellowColor];
        _number.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:_number];
        [_number release];
        
        self.likeCount = [[UILabel alloc] init];
        //        _likeCount.backgroundColor = [UIColor orangeColor];
        _likeCount.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:_likeCount];
        [_likeCount release];
        
        //        self.albumType = [[UILabel alloc] init];
        //        [self.contentView addSubview:_albumType];
    }
    return self;
}

- (void)setModel:(DvdDetalisModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        NSString *tempStr = _model.covers[0];
        [_album sd_setImageWithURL:[NSURL URLWithString:tempStr]];
        _topic.text = _model.name;
        _number.text = [NSString stringWithFormat:@"%@张图片", _model.count];
        _likeCount.text = [NSString stringWithFormat:@"%@人喜欢", _model.like_count];
        
    }
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    //    [super applyLayoutAttributes:layoutAttributes];
    __block __typeof(&*self)ws = self;
    [_album mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left).with.offset(0);
        make.right.equalTo(ws.mas_right).with.offset(0);
        make.top.equalTo(ws.mas_top).with.offset(0);
        make.height.equalTo(_album.mas_width);
    }];
    
    [_topic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_album.mas_bottom).with.offset(10);
        make.left.equalTo(ws.mas_left).with.offset(10);
        make.right.equalTo(ws.mas_right).with.offset(-10);
        make.height.equalTo(@20);
    }];
    
    //有BUG
    CGFloat textWidth = [[self class]widthWithText:[NSString stringWithFormat:@"%@张图片",_model.count]];
    NSLog(@"%lf", textWidth);
    [_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topic.mas_bottom).with.offset(5);
        make.left.equalTo(ws.mas_left).with.offset(10);
        make.width.equalTo(@(textWidth + 10));
        make.height.equalTo(@10);
    }];
    
    [_likeCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_number.mas_right).with.offset(0);
        make.top.equalTo(_topic.mas_bottom).with.offset(5);
        make.right.equalTo(ws.mas_right).with.offset(-10);
        make.height.equalTo(@10);
    }];
    
    //    [_albumType mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.size.mas_equalTo(CGSizeMake(80, 15));
    //        make.bottom.equalTo(ws.mas_bottom).with.offset(-10);
    //        make.left.equalTo(ws.mas_left).with.offset(10);
    //    }];
}

#pragma mark - 计算文本宽
+ (CGFloat)widthWithText:(NSString *)text
{
    //保存文本信息
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:11]};
    //设置最大范围
    CGSize size = CGSizeMake(150, 10);
    //算出文本需要的 rectangular
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 宽
    return rect.size.width;
}

@end
